<?php defined('SYSPATH') or die('No direct script access.');
/**
 * @file link.php
 * @brief 链接管理
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-02-16
 */
class Controller_Link extends Controller_Rest {
	public function before()
	{
		parent::before();
	}
	/**
	 * @brief 获取link 
	 *
	 * @param $link_id
	 *
	 * @return $this
	 */
	public function action_get($link_id = NULL)
	{
		if($link_id) $this->get_link($link_id);
		else $this->get_links();
	}
	/**
	 * @name  action_get 的两个私有函数
	 * @{ */

	/**
	 * @brief 获取指定id的link数据
	 *
	 * @param $link_id
	 *
	 * @return $this
	 */
	private function get_link($link_id)
	{
		$link = ORM::factory('link', $link_id);
		if(!$link->link_id)
		{
			$this->response->body('link not exists!')->status(202);
			return;
		}
		$res = json_encode($link->as_array());
		$this->response->body($res)->status(200);
	}
	/**
	 * @brief 获取指定起始位置的链接
	 *
	 * @return	$this 
	 */
	private function get_links()
	{
		/** 获取起止位置 */
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);
		$start = ($start) ? $start : 0;
		$pos = ($pos) ? $pos : PHP_INT_MAX;
		$link = ORM::factory('link');
		$links = $link
				->offset($start)
				->limit($pos)
				->find_all();
		if(!$links->count())
		{
			$this->response->body('no link found!')->status(202);
			return;
		}
		$res['num'] = $links->count();
		$res['list'] = array();
		foreach($links as $lik)
		{
			$res['list'][] = $lik->as_array();
		}
		$json_res = json_encode($res);
		$this->response->body($json_res)->status(200);
	}

	/**  @} */
	
	/**
	 * @brief 添加link
	 *
	 * @return $this
	 */
	public function action_post()
	{
		$link = ORM::factory('link');
		if(!$_POST)
		{
			$this->reponse->body('no link post!')->status(202);
			return;
		}
		if(isset($_POST['link_id']))
		{
			unset($_POST['link_id']);
		}
		try
		{
			$new_link = $this->request->post();
			$link_author = $new_link['user_id'];
			if($link_author)
			{
				/**匹配相应的user */
				$user = ORM::factory('user', $link_author);
				if(!$user->id)
				{
					$this->response->body('link_user not exist!')->status(206);
					return;
				}
			}
			$link->values($_POST)->check()->save();
			$this->response->body('link add success!')->status(200);
		}
		catch(ORM_Validation_Exception $e)
		{
			$errors = $e->errors();
			foreach($errors as $k=>$v)
			{
				$this->response->body($k.':not available')->status(206);
			}
		}
		catch(Exception $err)
		{
			$this->response->body($err->getMessage())->status(500);
		}
	}
	/**
		* @Brief action_put 更新link
		*
		* @Param $link_id
		*
		* @Returns 
	 */
	public function action_put($link_id)
	{
		$link = ORM::factory('link', $link_id);
		if(!$link->link_id)
		{
			$this->response->body('link not exist!')->status(202);
			return;
		}
		$new_link = $this->request->post();
		try
		{
			if($new_link)
			{
				$link_user = (isset($_POST['user_id'])) ? $new_post['user_id'] : 0;

				/**if author exist*/
				if($link_user)
				{
					$user = ORM::factory('user', $link_user);
					if(!$user->id)
					{
						$this->response->body('link_user not exists!')->status('206');
						return;
					}
				}
				/** 更新操作 */
				$link->values($new_link)->check()->update();
				$this->response->body('update success!')->status(200);
			}
			else
			{
				$this->response->body('json data not exists!')->status(202);
			}	
		}
		catch(ORM_Validation_Exception $e)
		{
			/** 验证错误 */
			$error = $e->errors();
			foreach($error as $k => $v)
			{
				$this->response->body($k.':not available')->status(206);
			}
		}
		catch(Exception $err)
		{
			$this->response->body($err->getMessage())->status(500);
		}
	}
	
	/**
	 * @brief 删除指定的link
	 *
	 * @param $link_id
	 *
	 * @return $this 
	 */
	public function action_delete($link_id)
	{
		$link = ORM::factory('link', $link_id);
		if(!$link->link_id)
		{
			$this->response->body('link not exist')->status(206);
			return;
		}
		try
		{
			/** 删除相应link下所有的linkmeta*/
			$linkmeta = $link->linkmeta->find_all();
			$query = DB::delete('linkmeta')->where('link_id', '=', $link_id);
			$query->execute();

			$link->delete();
			$this->response->body('link-'.$link_id.' delete success!')->status(200);
		}
		catch(Exception $err)
		{
			$this->response->body($err->getMessage())->status(500);
		}
	}
}
